import requests
import pandas as pd


def get_content():
    name_list = []
    renqi_list = []
    school_list = []
    school_add_list = []
    rank_list = []
    school_type_list = []
    key_list = ['1130', '107', '40']
    for key in key_list:
        renqi_url = f'https://api.zjzw.cn/web/api/counter?cid=2&did={key}&signsafe=9c8bad74c6d2414e6de774d4ce1e95d9'
        renqi = requests.get(renqi_url, headers=headers).json()['data'][key]
        if key == '1130':
            page_sum = 38
            name = '网络与新媒体'
        elif key == '107':
            page_sum = 32
            name = '新闻学'
        else:
            page_sum = 64
            name = '汉语言文学'
        print(key)
        for page in range(1, page_sum + 1):
            print(page)
            url = f'https://api.zjzw.cn/web/api/?is_single=2&local_province_id=44&page={page}&province_id=&request_type=1&size=10&special_id={key}&type=&uri=apidata/api/gk/special/school&signsafe=20175664b53a9ba204cbed7ec0d2a7f5'
            data = '{"is_single":2,"local_province_id":"44","page":%s,"province_id":"","request_type":1,"signsafe":"c72ce88bc6c3d00759575c8bacde1c5a","size":10,"special_id":"%s","type":"","uri":"apidata/api/gk/special/school"}' % (page, key)
            resp = requests.post(url, headers=headers, data=data).json()
            for each in resp['data']['item']:
                school = each['name']
                school_add = each['province_name']
                f211 = each['f211']
                f985 = each['f985']
                if f211 == 1 and f985 == 1:
                    type_2_9 = '211，985'
                elif f211 == 1:
                    type_2_9 = '211'
                elif f211 == 1:
                    type_2_9 = '985'
                else:
                    type_2_9 = ''
                type_ = each['level_name'] + '，' + each['type_name'] + '，' + each['nature_name'] + '，' + type_2_9 + '，' + each['dual_class_name']
                type_ = type_.replace('，，双一流', '，双一流').replace('，，', '')
                rank = each['ruanke_rank']
                print(school)
                name_list.append(name)
                renqi_list.append(renqi)
                school_list.append(school)
                school_add_list.append(school_add)
                rank_list.append(rank)
                school_type_list.append(type_)

    data_dic['专业名'] = name_list
    data_dic['人气值'] = renqi_list
    data_dic['学校名'] = school_list
    data_dic['学校排名'] = rank_list
    data_dic['学校类型'] = school_type_list
    data_dic['学校地址'] = school_add_list


if __name__ == '__main__':
    data_dic = {}
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
    }
    get_content()
    df = pd.DataFrame(data_dic)
    df.to_excel('result.xlsx', index=False)
    print('存储完毕！')
